import router from '@/router'

// state 存储数据
const state = {
  activeTabsValue:'/home',
  visitedViews: [],
  cachedViews: [],
}

// getters 对state数据进行加工
const getters ={
  activeTabsValue: state => state.activeTabsValue,
  visitedViews: state => state.visitedViews,
  cachedViews: state => state.cachedViews,
}

// actions 响应组件中的事件
const actions = {
  addVisitedView({ dispatch, commit }, view) {
    commit('ADD_VISITED_VIEW', view)
    dispatch('addActiveTabs',view.path)
    if(view.meta.keepAlive){
      dispatch('addCachedViews',view.path)
    }
  },
  addActiveTabs({commit}, path){
    commit('ADD_ACTIVE_TABS_VALUE',path)
  },
  addCachedViews({commit}, view){
    commit('ADD_CACHED_VIEW',view)
  },
  delView({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delVisitedView', view)
      dispatch('delCachedView', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delVisitedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_VISITED_VIEW', view)
      resolve([...state.visitedViews])
    })
  },
  delCachedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_CACHED_VIEW', view)
      resolve([...state.cachedViews])
    })
  },
  toLastView({dispatch, commit}, path){
    let index = state.visitedViews.findIndex(item => item.path == path)
    const nextTab = state.visitedViews[index + 1] || state.visitedViews[index - 1]
    if(!nextTab){
      router.push({path: '/'})
    }else{
      router.push({path: nextTab.path})
    }
    dispatch('addVisitedView',nextTab)
  },
  delOtherView({commit, state}, path){
    return new Promise(resolve => {
      commit('DEL_OTHER_VIEW',path)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delAllView({commit, state}){
    return new Promise(resolve => {
      commit('DEL_ALL_VIEW')
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  goHome({commit}){
    const toPath = '/home'
    commit('ADD_ACTIVE_TABS_VALUE',toPath)
    router.push({path: toPath})
  }
}

// mutations 操作数据
const mutations = {
  ADD_VISITED_VIEW: (state, view) => {
    if (state.visitedViews.some(v => v.path === view.path)) return
    state.visitedViews.push(
      Object.assign({}, view, {
        title: view.meta.title || 'no-name'
      })
    )
  },
  ADD_ACTIVE_TABS_VALUE: (state, path) =>{
    state.activeTabsValue = path
  },
  ADD_CACHED_VIEW: (state, view) => {
    if (state.cachedViews.includes(view)) return
    state.cachedViews.push(view)
  },
  DEL_VISITED_VIEW: (state, view) => {
    for (const [i, v] of state.visitedViews.entries()) {
      if (v.path === view) {
        state.visitedViews.splice(i, 1)
        break
      }
    }
  },
  DEL_CACHED_VIEW: (state, view) => {
    const index = state.cachedViews.indexOf(view)
    index > -1 && state.cachedViews.splice(index, 1)
  },
  DEL_OTHER_VIEW(state, path){
    state.visitedViews = state.visitedViews.filter(item =>{
      return item.path == path || item.meta.affix
    })
    state.cachedViews = state.cachedViews.filter(item =>{
      return item.path == path || item.meta.affix
    })
  },
  DEL_ALL_VIEW(state){
    state.visitedViews = state.visitedViews.filter(item => item.meta.affix)
    state.cachedViews = []
  }
}

// 暴露配置项在 index.js 自动生成对应的store
export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
}
